#include "Rectangle.h"

namespace forme
{
	// ctor
	Rectangle::Rectangle(float x, float y, float width, float height)
	{
		m_topLeft.x = x;
		m_topLeft.y = y;
		m_size.x = width;
		m_size.y = height;
	}

	Rectangle::Rectangle(Vector2D* topLeft, Vector2D* btmRight)
	{
		m_topLeft = *topLeft;
		m_size = Substract(btmRight, topLeft);
	}
	

	//return true if
	//the x distance between rect1 and rect2
	//is less or equal than
	//the half width of rect1 + the half width of rect2
	// and
	//idem for y ...
	//
	bool Rectangle::CollidesWith(const Rectangle* rect) const
	{
		bool hit = false;
		float length;
		float heigth;

		// affecte la difference positive
		length = m_topLeft.x > rect->GetX()
				?
					(m_topLeft.x + (m_size.x / 2)) - (rect->GetX() + (rect->GetWidth() / 2))
				:
					(rect->GetX() + (rect->GetWidth() / 2)) - (m_topLeft.x + (m_size.x / 2));

		// affecte la difference positive
		heigth = m_topLeft.y > rect->GetY()
				?
					(m_topLeft.y + (m_size.y / 2)) - (rect->GetY() + (rect->GetHeight() / 2))
				:
					(rect->GetY() + (rect->GetHeight() / 2)) - (m_topLeft.y + (m_size.y / 2));
					
		return	((2 * heigth) <= (rect->GetHeight() + m_size.y))
				&&
				((2 * length) <= (rect->GetWidth() + m_size.x));

	}

	bool Rectangle::Contains(float x, float y) const
	{
		return	(x + m_size.x >= m_topLeft.x && x <= m_topLeft.x + m_size.x)
				&&
				(y + m_size.y >= m_topLeft.y && y <= m_topLeft.y + m_size.y);
	}

	bool Rectangle::Contains(const Vector2D* point) const
	{
		return	(point->x + m_size.x >= m_topLeft.x && point->x <= m_topLeft.x + m_size.x)
				&&
				(point->y + m_size.y >= m_topLeft.y && point->y <= m_topLeft.y + m_size.y);
	}

	// getters
	float Rectangle::GetX() const
	{
		return m_topLeft.x;
	}

	float Rectangle::GetY() const
	{
		return m_topLeft.y;
	}

	float Rectangle::GetWidth() const
	{
		return m_size.x;
	}

	float Rectangle::GetHeight() const
	{
		return m_size.y;
	}

	// setters
	void Rectangle::SetPosition(Vector2D* vect)
	{
		m_topLeft.x = vect->x;
		m_topLeft.y = vect->y;
	}

	void Rectangle::SetPosition(float x, float y)
	{
		m_topLeft.x = x;
		m_topLeft.y = y;
	}

	// transform
	void Rectangle::MoveBy(Vector2D* vect)
	{
		m_topLeft.x += vect->x;
		m_topLeft.y += vect->y;
	}

	void Rectangle::MoveBy(float x, float y)
	{
		m_topLeft.x += x;
		m_topLeft.y += y;
	}
}